#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>
#include<QDebug>
musicslider::musicslider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::musicslider)
{
    ui->setupUi(this);
    currentPos = 0;
    maxWidth=width();
    moveSilder();
}

musicslider::~musicslider()
{
    delete ui;
}

void musicslider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSilder();
    qDebug()<<"mousePressEvent";

}

void musicslider::mouseMoveEvent(QMouseEvent *event)
{
    QRect rect = QRect(0, 0, width(), height());
    QPoint pos = event->pos();
    if(!rect.contains(pos))
    {
    return;
    }

    if(event->buttons() == Qt::LeftButton)
    {
    currentPos = event->pos().x();
    if(currentPos < 0)
    {
    currentPos = 0;
    }
    if(currentPos > maxWidth)
    {
    currentPos = maxWidth;
    }
    }
    moveSilder();
    qDebug()<<"mouseMoveEvent";

}

void musicslider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSilder();
    emit setMusicSliderPosition((float)currentPos / (float)maxWidth);
    qDebug()<<"mouseRelease";
}

void musicslider::moveSilder()
{

    ui->inslider->setMaximumWidth(currentPos);
    ui->inslider->setGeometry(0,0,currentPos,8);
}

void musicslider::setStep(float bf)
{
    currentPos = maxWidth*bf;
    moveSilder();
}
